package com.lancoo.answer.widget

import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvViewEditInputBinding
import com.lancoo.answer.util.DpUtils
import com.lancoo.answer.util.InputManager

/**
 * Created by wealive on 2021/9/14.
 *
 *
 */
class NewEditInputView(val type: String, context: Context) : DialogFragment(),
    View.OnClickListener, TextWatcher {
    var binding: EvViewEditInputBinding =
        EvViewEditInputBinding.inflate(LayoutInflater.from(context))

    private var stateListener: StateListener? = null

    override fun onStart() {
        super.onStart()
        val dialog = dialog
        if (dialog != null && context != null && dialog.window != null) {
            val window = dialog.window
            val attributes = window!!.attributes
            attributes.dimAmount = 0.1f
            attributes.gravity = Gravity.BOTTOM
            attributes.width = DpUtils.getWidthInPx(context)
            dialog.window!!.attributes = attributes
            this.dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.answerSheet_ColorStyle)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (dialog == null || dialog!!.window == null) {
            return
        }
        dialog!!.window!!.attributes.windowAnimations = R.style.answerFill_DialogAnimation
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView()
        super.onViewCreated(view, savedInstanceState)
    }


    private fun initView() {
        binding.edt.requestFocus()
        binding.edt.isFocusable = true
        binding.edt.setFocusableInTouchMode(true)
        binding.edt.addTextChangedListener(this@NewEditInputView)
        binding.ivClear.setOnClickListener(this@NewEditInputView)
        binding.tvConfirm.setOnClickListener(this@NewEditInputView)
        when (type) {
            "edit" -> binding.iv3rd.setImageResource(R.mipmap.ev_icon_gct_edit_black)
            "front" -> binding.iv3rd.setImageResource(R.mipmap.ev_icon_gct_add_front_black)
            "back" -> binding.iv3rd.setImageResource(R.mipmap.ev_icon_gct_add_back_black)
        }
        binding.iv3rd.tag = type
        isCancelable = true
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_confirm -> dismiss()
            R.id.iv_clear -> binding.edt.setText("")
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun show(manager: FragmentManager, tag: String?) {
        super.show(manager, tag)
//
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        stateListener?.close()
    }

    override fun afterTextChanged(s: Editable?) {
        if (s?.toString()!!.isEmpty()) {
            binding.ivClear.visibility = View.GONE
        } else {
            binding.ivClear.visibility = View.VISIBLE
        }
    }

    fun setStateListener(stateListener: StateListener) {
        this.stateListener = stateListener
    }

    interface StateListener {
        fun close()
    }

}